/*
 * Created on Mar 22, 2010
 */
package ro.letsdoitromania.forms;

import javax.microedition.lcdui.*;
import com.mgmaps.app.APPCommands;
import com.mgmaps.tools.StringTools;
import com.mgmaps.tools.UITools;
import com.mgmaps.utils.LS;
import ro.letsdoitromania.*;

public class DeleteConfirmForm implements CommandListener {
  private Form form;
  
  private static DeleteConfirmForm instance;
  private String language;
  
  private TrashPointData tpd;
  
  private StringItem siConfirm;
  
  public static DeleteConfirmForm getInstance() {
    if (instance == null)
      instance = new DeleteConfirmForm();
    return instance;
  }
  
  private DeleteConfirmForm() {
    language = Settings.language;
  }
  
  private void resetForm() {
    siConfirm = null;
  }
  
  public void show(TrashPointData p) {
    tpd = p;
    
    if (language != Settings.language)
      resetForm();
    
    if (siConfirm == null)
      siConfirm = new StringItem("", LS.get("ldir.del.confirm", new String[] {
          tpd.getDescription(), StringTools.dateTimeFormat(tpd.getDateTime()) }));
    
    form = UITools.buildForm(form, LS.get("ldir.del.title"), new Item[] { siConfirm },
        new Command[] { Commands.cinstance.deleteCommand, APPCommands.instance.cancelCommand },
        this);
    
    Display.getDisplay(LDIRMidlet.instance).setCurrent(form);
  }

  public void commandAction(Command cmd, Displayable disp) {
    if (cmd == Commands.cinstance.deleteCommand) {
      Settings.points.removeElement(tpd);
      Settings.updatePointIDs();
      ViewList.getInstance().populateList(tpd.getID()-2);
      
      Display.getDisplay(LDIRMidlet.instance).setCurrent(ViewList.getInstance().getList());
      SerialCall.instance.show(SerialCall.SC_ALERT, LS.get("ldir.del.title"), LS.get("ldir.del.done"));
    }
    else
      Display.getDisplay(LDIRMidlet.instance).setCurrent(ViewList.getInstance().getList());
  }
}
